package org.aicfve.config;

import com.sun.xml.internal.ws.model.ReflectAnnotationReader;
import org.aicfve.asset.AssetController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Properties;

@Component("storageConfig")
public class StorageConfig {

    @Value("${config.storage.savepath}")
    private String savepath;

    private String urlpath;

    // Getter and Setter
    public String getSavepath() {
        return savepath;
    }

    public void setSavepath(String savepath) {
        this.savepath = savepath;
    }

    public String getUrlpath() {
        if(urlpath != null)
            return urlpath;

        // 通过反射获取assets的url目录
        Class <AssetController> clazz = AssetController.class;

        // 检查是否具有RequestMapping
        if(!clazz.isAnnotationPresent(RequestMapping.class))
            throw new RuntimeException("Coding Error, can not find RequestMapping in Annoation");

        RequestMapping requestMapping = clazz.getAnnotation(RequestMapping.class);
        String [] values = requestMapping.value();
        if(values.length != 1)
            throw new RuntimeException("Coding Error, RequestMapping values length != 1");
        if(values[0] == null)
            throw new RuntimeException("Coding Error, RequestMapping value must not null");

        return values[0] + '/';
    }

    public void setUrlpath(String urlpath) {
        this.urlpath = urlpath;
    }
}